package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.IncomeBasicInfoMapper;
import com.koocloud.electroplatemanage.mapper.IncomePartJudgeMapper;
import com.koocloud.electroplatemanage.pojo.IncomeBasicInfoVo;
import com.koocloud.electroplatemanage.pojo.IncomePartJudgeVo;
import com.koocloud.electroplatemanage.pojo.WorkOrderListVo;
import com.koocloud.electroplatemanage.service.WorkOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020-11-24 15:46
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkOrderServiceImpl implements WorkOrderService {

    @Resource
    IncomePartJudgeMapper incomePartJudgeMapper;

    @Resource
    IncomeBasicInfoMapper incomeBasicInfoMapper;
    @Override
    public ResponseTemplate selectByOrderNo(String orderNumber) {
        //根据工单号查询台账表记录
        try {

            IncomePartJudgeVo incomePartJudgeVo = incomePartJudgeMapper.selectByWorkOrderNumber(orderNumber);
            IncomeBasicInfoVo incomeBasicInfoVo = incomeBasicInfoMapper.selectIncomeBasicInfo(orderNumber);



            if (!StringUtils.isEmpty(incomePartJudgeVo)&&StringUtils.isEmpty(incomeBasicInfoVo)) {

                //新建来料接收与判定实体
                WorkOrderListVo workOrderListVo = new WorkOrderListVo();
                workOrderListVo.setIncomeBasicInfoVo(incomeBasicInfoVo);
                workOrderListVo.setIncomePartJudgeVo(incomePartJudgeVo);
                //判断查表操作是否成功
                if (null != workOrderListVo) {
                    //查表成功返回数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(workOrderListVo).build();
                } else {
                    //查表失败返回提示
                    return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
                }
            }
            else {
                return ResponseTemplate.builder().code(1).message("来料接收与判定字段均未启用").count(0l).build();
            }
        }catch (Exception e) {
                //事务手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                //遇到异常，返回异常原因
                e.printStackTrace();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
    }
}
